program test

use bmad
use write_lat_file_mod
use random_mod
use ptc_layout_mod
!!!!! etienne's shit !!!!! 
USE run_madx 
USE pointer_lattice, latptc=>lat,dpe=>dp
implicit none
character*48 command_gino
type(layout), pointer :: e_ringt,p_ringt
TYPE(FIBRE),POINTER :: P,p1,p2,p12,p22
REAL(RP) a(3),b(3),d(3),ang(3),ent(3,3),exi(3,3),PIVOT(3),v ,thin
type(real_8) y1(6),y2(6)
type(damap) id
type(normalform) n1,n2
real(rp) x1(6),x2(6)
type(internal_state) state,STATE0
type(taylor) F,F_floquet
logical(lp) use_J

!!!!! end etienne's shit !!!!!


type (lat_struct) lat
type (ele_struct) ele3
type (coord_struct), allocatable :: orb(:)

real(rp) mat6(6,6), x(6), t,OMEGA(3)
integer i, j, ix, n_arg,mf,pos(2)
character(100) lat_file
character(120) nom(6)
type(pol_block) ks
!!!!! etienne's shit !!!!!
lmax=100.0_dpe
! use_info=.true.
!!!!! end etienne's shit !!!!!
write(6,*) " double ring"

lat_file = 'e_p_ring_ptc_like_combined.bmad'    !'lat.bmad'

call bmad_parser (lat_file, lat)

call lat_to_ptc_layout(lat)

goto 999
e_ringt=>m_t%start
p_ringt=>e_ringt%next
thin=1
CALL THIN_LENS_resplit(e_ringt,THIN)
CALL THIN_LENS_resplit(p_ringt,THIN)

ks=0
ks%ib_sol=1
ks%name='IPSOLE\1'

state=delta0

call init(state,3,1)

call alloc(y1);call alloc(y2);
call alloc(n1);call alloc(n2);
call alloc(id);call alloc(F,F_floquet)

x1=0.d0; x2=0.d0;
call find_orbit(e_ringt,x1,1,state,1.e-5_rp)
call find_orbit(p_ringt,x2,1,state,1.e-5_rp)

id=1

y1=id+x1; y2=id+x2;
e_ringt=ks  ! set the solenoid strenght in e-ring as a Taylor parameter

call track(e_ringt,y1,1,+state)
call track(p_ringt,y2,1,+state)

n1=y1; n2=y2;

write(6,*) " Q_x for e-ring"
call print(n1%dhdj%v(3),6)
write(6,*) " Q_x for p-ring"
call print(n2%dhdj%v(3),6)

f=(1.d0.mono.'2')  ! making x**2

call AVERAGE(F,n1%a_t,F_floquet,use_J=my_false)
write(6,*) " Beta_x for e-ring"
call print(F_floquet,6)



goto 999



999 command_gino="OPENGINO"    
  call context(command_gino)   ! context makes them capital
  call call_gino(command_gino)  
 111 command_gino="MINI" 
  call context(command_gino)   ! context makes them capital
  call call_gino(command_gino)  
 



!!!!!!!!! vaguelynecessary baloney
command_gino="CLOSEGINO"
  call call_gino(command_gino)  



 1112 call ptc_end

end program
